<?php
/*
 * Using Gepard as framework
 */
require_once("../kernel/init.php");
if( $Gepard->isconsole()){
	$Gepard->console->writeln("Cannot run Gepard installation script in console mode. Try run console.php");
	$Gepard->signal(_SIG_TERM);	
}
$language = (isset($_GET["language"]) && in_array($_GET["language"],$Gepard->get("syslanguages"))?$_GET["language"]:$Gepard->get("language"));
# checking for language in cookie
setcookie("language",$language);
$language = (isset($_COOKIE["language"]) )?($_COOKIE["language"]!=$language?$language:$_COOKIE["language"]):$language;
# setting for no messages on error, non-strict mode
//$Gepard->set("messageonerror",NO);
$file = file_get_contents("lang/".$language);
# parsing as lang file
$lang = $Gepard->lang->parse($file);
# preparing main installation object
require_once("install.object.php");
$install = new install($Gepard,$lang);
# preparing main template file
$template = file_get_contents("tpl/parent.html");
$errorstemplate = file_get_contents("tpl/error.html");
$error = null;
$useform = true;
$usesuccess = false;
# checking global configuration
$checkglobal = $install->checkglobal();
if ( !is_bool($checkglobal)){
	$error = $checkglobal;
	$useform = false;
}
# checking and prepare for major configuration
if( 
isset($_POST["Submit"]) &&
isset($_POST["dbo"])
	 && isset($_POST["dbo"]["engine"])
	 && isset($_POST["dbo"]["server"])
	 && isset($_POST["dbo"]["db"])
	 && isset($_POST["dbo"]["user"]) 
	 && isset($_POST["dbo"]["password"])  &&
isset($_POST["sys"])
 	&& isset($_POST["sys"]["name"])
 	&& isset($_POST["sys"]["language"]) &&
isset($_POST["admin"])
	&& isset($_POST["admin"]["email"])
	&& isset($_POST["admin"]["password"])	
	  ){
	  $url = preg_replace("/^(.*)installation(.*)$/i","$1",$_SERVER["REQUEST_URI"]);	
	  $preresult = $install->checkconf(
		$_POST["dbo"]["engine"],
		$_POST["dbo"]["server"],
		$_POST["dbo"]["db"],
		$_POST["dbo"]["user"],
		$_POST["dbo"]["password"],
		$_POST["sys"]["name"],
		$_POST["sys"]["language"],
		$url,
		$_POST["admin"]["email"],
		$_POST["admin"]["password"]	
		);
	if( !is_bool($preresult)){
		$error = $preresult;
		$useform = true;
	} else {
	
		$Gepard->set("exceptiononerror",true);
		try {	
			$install->sqlinstall();
			$install->writefiles();
			$usesuccess=true;
			$useform = false;
		}
		catch ( GepardException $e ){
			$error = $lang->get("error8").$e->getMessage();
		}
		$Gepard->set("exceptiononerror",false);			
	}
}
$errordata = null;
$formdata = null;
$successdata = null;
$tpltime = microtime(true);
if( !is_null($error)){
	$errordata = $Gepard->view->parse($errorstemplate,array("error"=>$error),$lang);
}
if( $useform == true) {
$dboengine = (isset($_POST["dbo"]["engine"])?$_POST["dbo"]["engine"]:0);
$dboserver = (isset($_POST["dbo"]["server"])?$_POST["dbo"]["server"]:"localhost");
$dbodb = (isset($_POST["dbo"]["db"])?$_POST["dbo"]["db"]:null);
$dbouser = (isset($_POST["dbo"]["user"])?$_POST["dbo"]["user"]:null);
$dbopassword = (isset($_POST["dbo"]["password"])?$_POST["dbo"]["password"]:null);
$sysname = (isset($_POST["sys"]["name"])?$_POST["sys"]["name"]:null);
$syslanguage = (isset($_POST["sys"]["language"])?$_POST["sys"]["language"]:$language);
$adminemail = (isset($_POST["admin"]["email"])?$_POST["admin"]["email"]:null);
$adminpassword = (isset($_POST["admin"]["password"])?$_POST["admin"]["password"]:null);
$templateform = file_get_contents("tpl/form.html");
$formdata =  $Gepard->view->parse($templateform,array("langs"=>$Gepard->get("syslanguages"),
"dboengine"=>$dboengine,
"dboserver"=>$dboserver,
"dbodb"		=>$dbodb,
"dbouser"	=>$dbouser,
"dbopassword"=>$dbopassword,
"syslanguage"=>$syslanguage,
"sysname"	=>$sysname,
"adminemail" =>$adminemail,
"adminpassword"=>$adminpassword
),$lang);}
if( $usesuccess ){
	$successdata = $Gepard->view->parse(file_get_contents("tpl/success.html"),array("url"=>$url),$lang);
}
$data = $successdata.$errordata.$formdata;
$template = $Gepard->view->parse($template,array("data"=>$data,"debug"=>preg_replace("/\n/","\\n",preg_replace("/\"|'/","",$Gepard->GetDebug(_STRING)))),$lang);
# trying to detect major system requirements
print $template;

?>
